package com.lc.pianostudent.view;

/**
 * Created by Administrator on 2017/3/27.
 */

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.lc.pianostudent.R;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

public class SelectPopupWindowNoPosition extends PopupWindow {
    private View mMenuView;
    private ViewPager viewpager;
    private int pos;
    private List<View> viewList = new ArrayList<>();
    private List<String> imList;
    private TextView indexTv;
    public Activity context1;
    private int posPop;

    public SelectPopupWindowNoPosition(Activity context, final List<String> imList, int posPop) {
        super(context);
        //去掉标题栏
//        context.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //数据
        context1 = context;
        this.imList = imList;
        this.posPop=posPop;
        LayoutInflater inflater = (LayoutInflater) context1
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.popup_window_no_position, null);
        Log.i("wq====2",imList.size()+"");
        Log.i("wq====1",posPop+"");
        for (int i = 0; i < imList.size(); i++) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_image_browse_vp, null);
            viewList.add(view);
        }
        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.FILL_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
//        this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
//        mMenuView.setOnTouchListener(new OnTouchListener() {
//            public boolean onTouch(View v, MotionEvent event) {
//                if (event.getAction() == MotionEvent.ACTION_UP) {
//                    dismiss();
//                }
//                return true;
//            }
//        });


        viewpager = (ViewPager) mMenuView.findViewById(R.id.image_browes_vp);
        indexTv = (TextView) mMenuView.findViewById(R.id.image_browes_index);
        //设置第几张
        indexTv.setText((posPop + 1) + "/" + imList.size());
        viewpager.setAdapter(new ImagebrowseAdapter());
        //设置ViewPager的选中项
        viewpager.setCurrentItem(posPop);
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                //更改选中第几张
                indexTv.setText((position + 1) + "/" + imList.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        viewpager.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    }

    class ImagebrowseAdapter extends PagerAdapter {

        //返回了当前要滑动视图的个数
        @Override
        public int getCount() {
            return viewList.size();
        }

        //ViewPager 是扩展于 ViewGroup，container参数是当前的ViewPager对象，所有的item都会被加入到ViewPager中，
        //position就是 每个item对应的下标
        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            //这个函数的实现的功能是创建指定位置的页面视图。适配器有责任增加即将创建的View视图到这里给定的container中，
            //这是为了确保在finishUpdate(viewGroup)返回时this is be done!
            //返回值：返回一个代表新增视图页面的Object（Key），这里没必要非要返回视图本身，
            //也可以这个页面的其它容器。其实我的理解是可以代表当前页面的任意值，只要你可以与你增加的View一一对应即可，
            //比如position变量也可以做为Key

            container.addView(viewList.get(position));
            //获取当前显示的图片
            Log.i("wq====", viewList.size() + "=====" + imList.size());
            final TouchImageView imageView = (TouchImageView) viewList.get(position).findViewById(R.id.image_browes_ig);
            Picasso.with(context1).load(imList.get(position)).into(imageView);
            imageView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
            //返回是哪一个视图
            return viewList.get(position);
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {

            return arg0 == arg1;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            //把销毁的视图从视图集合中移除,将给定位置的视图从container中移除了……(滑动时,把上一个视图从container视图group中移除)
            container.removeView(viewList.get(position));
        }
    }

}

